<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { deletePatientAPI, getPatientMyListAPI } from '@/api/user'
import { useConsultStore } from '@/store/consult'
import type { GetPatientItemRes } from '@/types/user'
import { showModal } from '@/utils/modal'
import { onPageShow, onReady } from '@dcloudio/uni-app'
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

// 患者列表信息
const patientList = ref<GetPatientItemRes[]>([] as GetPatientItemRes[])
// 查询患者列表信息
const getPatientMyList = async () => {
  const res = await getPatientMyListAPI()
  patientList.value = res
}
// getPatientMyList()

// 添加患者
const handleAddPatient = () => {
  uni.navigateTo({
    url: '/subpkg_archive/form/form',
  })
}

// 删除患者
const handleDeletePatient = async (id: string) => {
  // 二次确认
  const res = await showModal({
    title: '提示',
    content: '确定删除该患者吗？',
  })
  if (res.confirm) {
    await deletePatientAPI(id)
    store.patientID = ''
    await getPatientMyList()
    uni.showToast({
      title: '删除成功',
      icon: 'success',
    })
  }
}

onPageShow(() => {
  console.log('onPageShow')
  getPatientMyList()
})

// 获取地址栏信息
const props = defineProps<{ type?: string }>()
onReady(() => {
  if (props.type === 'consult') {
    uni.setNavigationBarTitle({
      title: '选择患者',
    })
  }
})

// 点击患者
const store = useConsultStore()
const onClick = (id: string) => {
  if (props.type === 'consult') {
    store.patientID = id
    console.log('点击患者', store.patientID)
  }
}

// 下一步
const handleNext = () => {
  uni.navigateTo({
    url: '/subpkg_consult/payment/payment',
  })
}
</script>

<template>
  <scroll-view>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>

      <uni-swipe-action v-for="item in patientList" :key="item.id">
        <uni-swipe-action-item
          :right-options="swipeOptions"
          :auto-close="true"
          @click="handleDeletePatient(item.id)"
        >
          <view
            class="archive-card"
            :class="{
              active: props.type === 'consult' ? store.patientID === item.id : item.defaultFlag,
            }"
            @click="onClick(item.id)"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text v-if="item.defaultFlag" class="default">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator
              hover-class="none"
              class="edit-link"
              :url="`/subpkg_archive/form/form?id=${item.id}&patientInfo=${encodeURIComponent(
                JSON.stringify(item)
              )}`"
            >
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="patientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/form">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label" @click="handleAddPatient">添加患者</text>
        </navigator>
      </view>
    </view>

    <view class="next-step" v-if="props.type === 'consult'">
      <button :disabled="!store.patientID" class="uni-button" @click="handleNext">下一步</button>
    </view>
  </scroll-view>
</template>

<style lang="scss">
@import './index.scss';
</style>
